package com.example.mdm4

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.launcher.ARouter
import com.example.lib_base.BaseActivity
import com.example.mdm4.databinding.ActivityMainBinding
import com.example.mdm4.main.MainState
import com.example.mdm4.main.MainViewModel
import com.example.mdm4.main.WhiteUrEntity
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch


class MainActivity : BaseActivity<ActivityMainBinding,MainViewModel>() {
    override fun createVM(): MainViewModel {
        return MainViewModel()
    }

    override fun createV(): ActivityMainBinding {
        return ActivityMainBinding.inflate(layoutInflater)
    }

    override fun bindState() {
        lifecycleScope.launch {
            vm.mutableContainer.uiFlow.collect{
                val state = it as MainState
                if(state.lockState){
                    Toast.makeText(this@MainActivity, "联系管理员解锁", Toast.LENGTH_SHORT).show()
                    finish()
                }else if(state.whiteState){
                    val list = ArrayList<WhiteUrEntity>()
                    state.datas.forEach {
                        list.add(it)
                        Log.i("===",it.url)
                    }
                    val async = async { delay(3000) }
                    async.await()
                    ARouter.getInstance().build("/app/HomeActivity").navigation()
                    finish()
                }
            }
        }
    }

    override fun initData() {
        vm.selWhite()
    }

    override fun initView() {
    }
}